package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

import java.util.Arrays;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/reordered-power-of-2/">869. 重新排序得到 2 的幂</a>
 */
public class Solution869 extends BaseSolution {

    public boolean reorderedPowerOf2(int n) {
        int[] origin = new int[10];
        check(n, origin);

        int t = 1;
        int[] tmp = new int[10];
        while (t < 1000000001) {
            Arrays.fill(tmp, 0);
            check(t, tmp);
            if (isOK(origin, tmp)) {
                return true;
            }
            t *= 2;
        }
        return false;
    }

    private boolean isOK(int[] origin, int[] tmp) {
        for (int i = 0; i < origin.length; i++) {
            if (origin[i] != tmp[i]) {
                return false;
            }
        }
        return true;
    }

    private void check(int n, int[] count) {
        while (n > 0) {
            count[n % 10] ++;
            n /= 10;
        }
    }

    public static void main(String[] args) {
        Solution869 solution = new Solution869();
    }
}